package com.bcmusic.search.controller;

import com.bcmusic.music.feign.MusicFeign;
import com.bcmusic.music.pojo.Music;
import com.bcmusic.search.service.ESService;
import com.bcmusic.search.vo.AuthorizationVO;
import com.bcmusic.search.vo.CopyrightResult;
import com.bcmusic.search.vo.MusicVO;
import entity.Result;
import entity.StatusCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @ClassName : ESController  //类名
 * @Description : 提供各种搜索相关的接口  //描述
 * @Author : YG  //作者
 * @Date: 2020-10-26 10:03  //时间
 */
@Api(value = "/search", tags = "搜索模块")
@RestController
@RequestMapping("/search")
public class ESController {

    @Autowired
    private ESService esService;

    @Autowired
    private MusicFeign musicFeign;

//    @GetMapping("/copyright")
//    public Result<List<Map<String, Object>>>getCopyright(@RequestParam("musicName") String musicName,
//                                                         @RequestParam("author") String author,
//                                                         @RequestParam("user") String user,
//                                                         @RequestParam("pageSize") Integer pageSize,
//                                                         @RequestParam("pageNum") Integer pageNum) throws Exception{
//        List<Map<String, Object>> copyright = esService.getCopyright(musicName, author, user, pageSize, pageNum);
//
//        return new Result<>(true, StatusCode.OK,"版权信息查询成功",copyright);
//    }

    @ApiOperation(value = "模糊查询版权信息", notes = "模糊查询版权信息")
    @GetMapping("/fuzzySearch")
    public Result<List<Map<String, Object>>>fuzzySearch(@RequestParam(value = "musicName",required = false)@ApiParam("音乐名称") String musicName,
                                                     @RequestParam(value = "lyricist",required = false)@ApiParam("作曲家") String lyricist,
                                                     @RequestParam(value = "singer",required = false)@ApiParam("歌手") String singer,
                                                     @RequestParam(value = "composer",required = false)@ApiParam("作词家") String composer) throws Exception {

        List<Map<String, Object>> musics = esService.getMusic(musicName,singer,lyricist,composer);

        for (int i = 0; i < musics.size(); i++) {
            musics.get(i).put("authorizations",esService.getCopyright((Integer) musics.get(i).get("musicId")));
        }

        return new Result<>(true,StatusCode.OK,"音乐查询成功",musics);
    }


    @ApiOperation(value = "精确查询版权信息", notes = "精确查询版权信息")
    @GetMapping("/preciseSearch")
    public Result<List<Map<String, Object>>>preciseSearch(@RequestParam(value = "authorizationCode")@ApiParam("授权码") String authorizationCode,
                                                     @RequestParam(value = "registrationCode")@ApiParam("注册码") String registrationCode)
            throws Exception {

        List<Map<String, Object>> musics = esService.preciseQueryMusic(registrationCode);

        for (int i = 0; i < musics.size(); i++) {
            musics.get(i).put("authorizations",esService.preciseQueryAuthorization(authorizationCode));
        }
        return new Result<>(true,StatusCode.OK,"音乐查询成功",musics);
    }

    /**
     * 手动调用同步数据库先,测试
     * @return
     */
    @GetMapping("upload/copyright")
    public Result uploadCopyright() throws ParseException {
        List<AuthorizationVO>list=new ArrayList<>();
        DateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM-dd");
        list.add(new AuthorizationVO(28,"1B936D0F719B0ACECB265FBFC3EA7AF456C538950DA14F7302A5079B9AED2353","许可","{发行权，改编权，著作权}","国内",dateFormat1.parse("2020-11-10"),dateFormat1.parse("2020-12-06"),"林梓成",1));
        list.add(new AuthorizationVO(29,"A164899AC5CE7AFA9C54235756520C7DA035EBD4EABCCDDD5D9142821BC182FD","许可","{表演权}","国外",dateFormat1.parse("2020-11-12"),dateFormat1.parse("2020-12-12"),"林梓成",1));
        list.add(new AuthorizationVO(30,"6240B16F2EBA9B4CB32F3D1CE5DE07E00A9FA7F3DC5A3AE6D61505B8C96012F8","许可","{出租权，放映权}","国内",dateFormat1.parse("2020-11-12"),dateFormat1.parse("2020-11-12"),"林梓成",1));

//        list.add(new MusicVO("3F65CE9DD875851FD3C2749B25E2AC30936A466A80C976704F11ECA77A71976E", "梁华勇", 28,"《Track》","a","b","c","202000000015", 1,"Dawn","FB1A04945D0A0F4C889EE93EA45D1E973960359A48D08840FE043B0AA99C1204","","","","", "2020-10-29 20:43:14"));
//        list.add(new MusicVO("81276C8BBD9059B77501C366BD0B42383F81858F35051839C5845CFF88022DE0", "梁华勇", 29,"《perfectionism》","a","b","c","202000000015", 1,"","4B4B1860F4058323AC5E3079E677A0B863561A694BD8B868B075E1CE2149C9E0","yg","","","", "2020-10-30 19:14:19"));
//        list.add(new MusicVO("5D995007563F74C8A58299C68FE116432F241AF40ADC1492C481CDBE9263BD04", "梁华勇", 30,"《heart》","a","b","c","202000000015", 1,"yg","FB1A04945D0A0F4C889EE93EA45D1E973960359A48D08840FE043B0AA99C1204","","","","", "2020-10-2920:43:14"));




        esService.bulkSave("authorization",list);
        return new Result(true,StatusCode.OK,"添加成功");
    }

    /**
     * 手动调用同步数据库先，测试
     * @return
     */
    @GetMapping("upload/music")
    public Result uploadmusic()
    {
        List<CopyrightResult>list=new ArrayList<>();
        List<Music> data = musicFeign.findAllMusic().getData();
        esService.bulkSave("music",data);
        return new Result(true,StatusCode.OK,"添加成功");
    }



}
